package com.qf.hospital.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.entity.SysMenu;
import com.qf.hospital.entity.SysMenuExample;
import com.qf.hospital.enums.HospitalExceptionEnums;
import com.qf.hospital.exception.HospitalException;
import com.qf.hospital.mapper.SysMenuMapper;
import com.qf.hospital.service.SysMenuService;
import com.qf.hospital.utils.ExampleUtil;
import com.qf.hospital.vo.SysMenuVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ASUS
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.service.impl
 * @Date 2021/10/19 11:12
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public List<SysMenu> findAll() {
        return sysMenuMapper.selectByExample(null);
    }

    /**
     * 展示table数据
     * @param title
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<SysMenu> queryMenuAllList(String title,Integer id, Integer page, Integer limit) {
        //1、执行分页
        PageHelper.startPage(page,limit);
        //2、封装查询条件
        SysMenuExample sysMenuExample = new SysMenuExample();
        if(!StringUtils.isEmpty(title)){
            sysMenuExample.createCriteria().andTitleLike(ExampleUtil.any(title));
        }
        if(id != null){
            sysMenuExample.createCriteria().andIdEqualTo(id);
        }
        //3、查询数据
        List<SysMenu> sysMenuList = sysMenuMapper.selectByExample(sysMenuExample);
        //4、封装数据
        PageInfo<SysMenu> pageInfo = new PageInfo<>(sysMenuList);
        //5、返回
        return pageInfo;
    }

    /**
     * 添加菜单
     * @param sysMenu
     */
    @Override
    public void addMenu(SysMenu sysMenu) {
        int count = sysMenuMapper.insertSelective(sysMenu);
        if(count != 1){
            log.error("【添加菜单信息】 添加失败！sysMenu={}",sysMenu);
            throw new HospitalException(HospitalExceptionEnums.MENU_ADD_ERROR);
        }
    }

    /**
     * 修改菜单
     * @param sysMenu
     */
    @Override
    public void updateMenu(SysMenu sysMenu) {
        int count = sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
        if(count != 1){
            log.error("【修改菜单信息】 修改失败！sysMenu={}",sysMenu);
            throw new HospitalException(HospitalExceptionEnums.MENU_UPDATE_ERROR);
        }
    }

    /**
     * 查询是否有子节点
     * @param id
     * @return
     */
    @Override
    public Boolean checkMenuHasChildren(Integer id) {
        SysMenuExample sysMenuExample = new SysMenuExample();
        sysMenuExample.createCriteria().andParentIdEqualTo(id);
        List<SysMenu> sysMenuList = sysMenuMapper.selectByExample(sysMenuExample);
        if(sysMenuList != null && sysMenuList.size() == 1){
            return true;
        }
        return false;
    }

    /**
     * 删除菜单
     * @param id
     */
    @Override
    public void deleteMenu(Integer id) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(id);
        sysMenu.setAvailable(0);
        int count = sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
        if(count != 1){
            log.error("【删除菜单信息】 删除失败！sysMenu={}",sysMenu);
            throw new HospitalException(HospitalExceptionEnums.MENU_DELETE_ERROR);
        }
    }
}
